Ext.namespace('Pearlsen');
Pearlsen.Constant = {
	PEARLSOFT_USERNAME:'PEARLSOFT_USERNAME',
	PEARLSOFT_USERCODE:'PEARLSOFT_USERCODE',
	/**
	 * 用户状态_属性
	 * @param {Object} msg
	 * @return {TypeName} 
	 */
	USER_STATE:'USER_STATE',
	
	USER_STATE_UNLOGIN:'0',
	//用户状态：登入锁定
	USER_STATE_LOGIN:'1',
	//用户状态：锁定
	USER_STATE_LOCK:'2'
	
	
}

Pearlsen.Utils = {

	/**
	 * 警告信息
	 */
	showWarnMsg:function(msg){
		Pearlsen.ex.Tip.msg("", Ext.getDom('msgDiv').innerHTML, "messageWarning", msg);
		return;
	},
	/**
	 * 提示信息
	 */
	showInfoMsg:function(msg){
		Pearlsen.ex.Tip.msg("", Ext.getDom('msgDiv').innerHTML, "messageInfo", msg);
		return;
	},
	/**
	 * 错误提示信息
	 */
	showErrorMsg:function(msg){
		Pearlsen.ex.Tip.msg("", Ext.getDom('msgDiv').innerHTML, "messageError", msg);
		return;
	},
	/**
	 * 成功提示信息
	 */
	showSuccessMsg:function(msg){
		Pearlsen.ex.Tip.msg("", Ext.getDom('msgDiv').innerHTML, "messageSuccess", msg);
		return;
	},
	
	destroyLockWin:function(){
		var lockWin = Ext.getCmp("lockWin");
		if(lockWin){
			lockWin.destroy();
		}
		Pearlsen.Utils.cookie.set(Pearlsen.Constant.USER_STATE,Pearlsen.Utils.base64.encode(Pearlsen.Constant.USER_STATE_UNLOGIN));
	},
	/**
	 * 显示屏幕
	 */
	showLock:function(){
		var state = Pearlsen.Utils.base64.decode(this.cookie.get(Pearlsen.Constant.USER_STATE));
		var userName = Pearlsen.Utils.base64.decode(Pearlsen.Utils.cookie.get(Pearlsen.Constant.PEARLSOFT_USERCODE));
		if(state==Pearlsen.Constant.USER_STATE_LOCK){
			//var lockWin = Ext.create()
			var lockForm = Ext.create('Pearlsen.form.PPanel',{
			items:[{
		            fieldLabel:'密码',
		            id:'l_password',
		            name: 'l_password',
		            inputType : "password",
		            xtype:'textfield',
		            allowBlank:false
        	  },{
        		  xtype:'hiddenfield',
        		  id:'l_usercode',
		          name: 'l_usercode',
		          value:userName 
        	  }]
			});
			var lockWin =  Ext.create('widget.window',{
				layout:'fit',
				id:"lockWin",
				name:"lockWin",
				modal:true,
				closable:false,
				title:'请输入密码解锁',
				items:lockForm,
				buttons:[{
					text:'解锁',
					handler:function(){
						var _username = Ext.getCmp("l_usercode").value;
						var _password = Ext.getCmp("l_password").value;
						var query=new Query('SysUserLoadHandler','SysUserDataSetHandler');
						query.setHandlerMethod("doLogin");
						query.addCondition("user_name",_username);
						query.addCondition("user_password",_password);
						var ds=query.load();
						if(ds==null||ds.size()==0){
							Pearlsen.Utils.showErrorMsg("解锁失败");
						}else{
							ds.next();
							if(ds.getValue("status")=='success'){
								lockWin.destroy();
							}
							else{
								Pearlsen.Utils.showErrorMsg("解锁失败");
							}
						}
					
				
				}
				}]
			}).show();
			
		}
		
		
		
	},
	base64:{
		base64EncodeChars : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
	 	base64DecodeChars :new Array(
		    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
		    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
		    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
		    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
		    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1),
		encode:function (str) {
			    var out, i, len;
			    var c1, c2, c3;
			
			    len = str.length;
			    i = 0;
			    out = "";
			    while(i < len) {
			    c1 = str.charCodeAt(i++) & 0xff;
			    if(i == len)
			    {
			        out += this.base64EncodeChars.charAt(c1 >> 2);
			        out += this.base64EncodeChars.charAt((c1 & 0x3) << 4);
			        out += "==";
			        break;
			    }
			    c2 = str.charCodeAt(i++);
			    if(i == len)
			    {
			        out += this.base64EncodeChars.charAt(c1 >> 2);
			        out += this.base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
			        out += this.base64EncodeChars.charAt((c2 & 0xF) << 2);
			        out += "=";
			        break;
			    }
			    c3 = str.charCodeAt(i++);
			    out += this.base64EncodeChars.charAt(c1 >> 2);
			    out += this.base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
			    out += this.base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
			    out += this.base64EncodeChars.charAt(c3 & 0x3F);
			    }
			    return out;
			},
			decode:function(str) {
			    var c1, c2, c3, c4;
			    var i, len, out;
			
			    len = str.length;
			    i = 0;
			    out = "";
			    while(i < len) {
			    /* c1 */
			    do {
			        c1 = this.base64DecodeChars[str.charCodeAt(i++) & 0xff];
			    } while(i < len && c1 == -1);
			    if(c1 == -1)
			        break;
			
			    /* c2 */
			    do {
			        c2 = this.base64DecodeChars[str.charCodeAt(i++) & 0xff];
			    } while(i < len && c2 == -1);
			    if(c2 == -1)
			        break;
			
			    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
			
			    /* c3 */
			    do {
			        c3 = str.charCodeAt(i++) & 0xff;
			        if(c3 == 61)
			        return out;
			        c3 = this.base64DecodeChars[c3];
			    } while(i < len && c3 == -1);
			    if(c3 == -1)
			        break;
			
			    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
			
			    /* c4 */
			    do {
			        c4 = str.charCodeAt(i++) & 0xff;
			        if(c4 == 61)
			        return out;
			        c4 = this.base64DecodeChars[c4];
			    } while(i < len && c4 == -1);
			    if(c4 == -1)
			        break;
			    out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
			    }
			    return  Pearlsen.Utils.utf8to16(out);
			}
		
	},
	utf8to16:function (str) {
	    var out, i, len, c;
	    var char2, char3;
	
	    out = "";
	    len = str.length;
	    i = 0;
	    while(i < len) {
	    c = str.charCodeAt(i++);
	    switch(c >> 4)
	    {
	      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
	        // 0xxxxxxx
	        out += str.charAt(i-1);
	        break;
	      case 12: case 13:
	        // 110x xxxx 10xx xxxx
	        char2 = str.charCodeAt(i++);
	        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
	        break;
	      case 14:
	        // 1110 xxxx 10xx xxxx 10xx xxxx
	        char2 = str.charCodeAt(i++);
	        char3 = str.charCodeAt(i++);
	        out += String.fromCharCode(((c & 0x0F) << 12) |
	                       ((char2 & 0x3F) << 6) |
	                       ((char3 & 0x3F) << 0));
	        break;
	    }
	    }
	
	    return out;
	},
	utf16to8:function (str) {
	    var out, i, len, c;
	
	    out = "";
	    len = str.length;
	    for(i = 0; i < len; i++) {
	    c = str.charCodeAt(i);
	    if ((c >= 0x0001) && (c <= 0x007F)) {
	        out += str.charAt(i);
	    } else if (c > 0x07FF) {
	        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
	        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
	        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
	    } else {
	        out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
	        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
	    }
	    }
	    return out;
	},
	cookie:{
		set:function(name,value){
			var argv = arguments;
			var argc = arguments.length;
			var expires = (argc > 2) ? argv[2] : null;
			var path = (argc > 3) ? argv[3] : '/';
			var domain = (argc > 4) ? argv[4] : null;
			var secure = (argc > 5) ? argv[5] : false;
			document.cookie = name
				+ "="
				+ encodeURIComponent(value)
				+ ((expires == null) ? "" : ("; expires=" + expires
						.toUTCString()))
				+ ((path == null) ? "" : ("; path=" + path))
				+ ((domain == null) ? "" : ("; domain=" + domain))
				+ ((secure == true) ? "; secure" : "");
		
		},
		get:function(name){
			var arg = name + "=";
			var alen = arg.length;
			var clen = document.cookie.length;
			var i = 0;
			var j = 0;
			while (i < clen) {
				j = i + alen;
				if (document.cookie.substring(i, j) == arg)
					return this.getCookieVal(j);
				i = document.cookie.indexOf(" ", i) + 1;
				if (i == 0)
					break;
			}
		return null;
		},
		getCookieVal:function(offset){
			var endstr = document.cookie.indexOf(";", offset);
			if (endstr == -1) {
				endstr = document.cookie.length;
			}
			return decodeURIComponent(document.cookie.substring(offset, endstr));
		},
		clear:function(name){
			if (Cookies.get(name)) {
			document.cookie = name + "="
					+ "; expires=Thu, 01-Jan-70 00:00:01 GMT";
			}
		}
		
		
	},
	addTab:function(id,title,url){
		parent.MainTab.addTab(id,title,url);
		/*
		var tabs=window.parent.mainTabPanel;
		if(!tabs){
			Pearlsen.Utils.showErrorMsg("mainTabPanel不存在");
			return ;
		}
		tabs.add({
	    		margins:'0 0 0 0',
	            title: title,
	            id:id,
	            closable: true,
	            html: "<iframe id='"+title+"_tabs' src='"+Pls.Webpath+url+"?menuId="+id+"' width='100%' height='100%' ></iframe>"
	        }).show();
		*/
	},
	closeTab:function(){
		var mainTabPanel=parent.MainTab.getMainTab();
		if(!mainTabPanel){
			Pearlsen.Utils.showErrorMsg("mainTabPanel不存在");
			return ;
		}
		mainTabPanel.getActiveTab().destroy(); 
		/*
		var tabs=window.parent.mainTabPanel;
		if(!tabs){
			Pearlsen.Utils.showErrorMsg("mainTabPanel不存在");
			return ;
		}
		tabs.getActiveTab().destroy(); 
		*/
	},
	openWindow:function(id,title,url,modal,width,height){
		if(width == undefined){
			width=500;
		}
		if(height == undefined){
			height=500;
		}
		if(modal == undefined){
			modal=false;
		}
		var win = Ext.create('Ext.window.Window',{
			title:title,
			width:width,
			height:height,
			layout:'fit',
			isTopContainer:true,
			modal:modal,
			html: "<iframe id='"+title+"_tabs' src='"+Pls.Webpath+url+"?menuId="+id+"' width='100%' height='100%' ></iframe>"
		});
		win.show();
		return win;
	}
	
	
};

Ext.onReady(function(){
	Pearlsen.Utils.showLock();
	
},2000);
